package ictbk.ttmk51.grapro.app.common;

import java.util.Calendar;
import java.util.UnknownFormatFlagsException;

import ictbk.ttmk51.grapro.app.utils.DateFormat;
import ictbk.ttmk51.grapro.app.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

public class MyActivity extends Activity {
	public ProgressDialog showProgressDialog(String title, String msg, int icon, boolean cancelable){
		ProgressDialog pd = new ProgressDialog(this);
		if(title != null){
			pd.setTitle(title);
		}
		if(msg != null){
			pd.setMessage(msg);
		}
		if(icon != -1){
			pd.setIcon(icon);
		}
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	/**
	 * Get system date
	 * 
	 * @param format
	 * @return
	 */
	public String getDate(DateFormat format, String separater) {
		if (separater == null) {
			throw new UnknownFormatFlagsException("unsupported for separater '" + separater + "'");
		}
		Calendar c = Calendar.getInstance();
		int mYear;
		int mMonth;
		int mDay;

		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH) + 1;// Months from the calendar are
		// offset by one. Add one if you
		// want human readable.
		mDay = c.get(Calendar.DAY_OF_MONTH);

		if (format == DateFormat.DDMMYYYY) {
			return mDay + separater + mMonth + separater + mYear;
		} else if (format == DateFormat.MMDDYYYY) {
			return mMonth + separater + mDay + separater + mYear;
		} else if (format == DateFormat.YYYYMMDD) {
			return mYear + separater + mMonth + separater + mDay;
		} else {
			return mDay + separater + mMonth + separater + mYear;
		}
	}
	
	/**
	 * Get system time
	 * 
	 * @return
	 */
	public String getTime() {
		Calendar c = Calendar.getInstance();

		int h;
		int m;
		int s;

		h = c.get(Calendar.HOUR_OF_DAY);
		m = c.get(Calendar.MINUTE);
		s = c.get(Calendar.SECOND);
		return h + ":" + m + ":" + s;
	}
	
	public String getDATE_TIME(DateFormat format, String separater){
		return getDate(format, separater) + " at " + getTime();
	}
	
	public AlertDialog showMessageDialog(String title, String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.info_icon);
		if (title == null) {
			return builder.setMessage(msg).show();
		} else {
			return builder.setMessage(msg).setTitle(title).show();
		}
	}
	
	public ProgressDialog showProgressDialog(String msg, boolean cancelable){
		ProgressDialog pd = new ProgressDialog(this);
		if(msg != null)
			pd.setMessage(msg);
		pd.setIcon(R.drawable.process_icon);
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	public ProgressDialog showProgressDialog(String title, String msg, boolean cancelable) {
		ProgressDialog pd = new ProgressDialog(this);
		if (title != null) {
			pd.setTitle(title);
		}
		if (msg != null)
			pd.setMessage(msg);
		pd.setIcon(R.drawable.process_icon);
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	protected void showToast(int msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
	}
	
	protected void showToast(String msg){
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
	}
	
	protected void showToast(String msg, int duaration){
		Toast.makeText(getBaseContext(), msg, duaration).show();
	}
	
	protected void showMessageDialog(String title, int icon, String msg){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(icon);
		if(title == null){
			builder.setMessage(msg).show();
		}else{
			builder.setTitle(title).setMessage(msg).show();
		}
	}
	
	protected AlertDialog showConfirmDiaglog(String title, String msg, OnClickListener yesClick){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.question_icon);
		if(title != null)
			builder.setTitle(title);
		if(msg != null)
			builder.setMessage(msg);
		return builder.setPositiveButton("Yes", yesClick).setNegativeButton("No", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		}).show();
	}
	
	public AlertDialog showConfirmDialog(String title, String msg, OnClickListener yesClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.question_icon);
		if(title!=null) builder.setTitle(title);
		if(msg!=null) builder.setMessage(msg);
		return builder.setPositiveButton("Yes", yesClick).setNegativeButton("No", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).show();
	}
	
	public AlertDialog showErrorDialog(String msg){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.error_icon);
		builder.setMessage(msg);
		builder.setTitle("Error!");
		builder.setPositiveButton("OK", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		return builder.show();
	}
}
